package com.xexun.gpstracker.pojo;

import java.io.Serializable;

import android.content.Context;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import com.xexun.gpstracker.AppContext;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.common.CommonRestPath;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.inter.HttpCallback;
import com.xexun.gpstracker.util.HttpUtil;
import com.xexun.gpstracker.util.JsonHttpResponseHelper;

/**
 * 监护人实体
 *
 * @author huangfei
 */
public class Keeper extends ManaRelation implements Serializable {

    private static final long serialVersionUID = -6806294559373705721L;
    /**
     * "id": 1,
     * "name": "13526236236",
     * "portrait": " http://114.119.7.105/gps/uploadsimgs/user/5652365985556956.jpg ",
     * "passward": "WDsdsawfSFSFerwweeeedfa",
     * "appIdentity": "爷爷",
     * "appUserNum": “2”
     */
    private int id = 0;
    private String name = "";//电话号码
    private String portrait = "";//头像
    private String password = "";
    private String appIdentity = "";    //关系
    private String appUserNum = "";
    private String cellPhone = "";
    private String oldCellPhone = "";
    private String nickName = "";
    private String validcode = "";
    private String imei = "";
    private int relation = 0;
    private int watchRegion = 0;//手表使用区域，0为默认，1为中国，2为非中国
    private int setAsAdmin=0;

    public Keeper() {
    }

    public Keeper(JSONObject json) {
        try {
            if (json.has("id")) id = json.optInt("id", 0);
            if (json.has("name")) name = json.optString("name");
            if (json.has("portrait")) portrait = json.optString("portrait");
            if (json.has("passward")) password = json.optString("passward");
            if (json.has("appIdentity")) appIdentity = json.optString("appIdentity");
            if (json.has("appUserNum")) appUserNum = json.optString("appUserNum");
            if (json.has("cellPhone")) cellPhone = json.optString("cellPhone");
            nickName = json.optString("nickName");
            imei = json.optString("imei");
            relation = json.optInt("relation");
            watchRegion = json.optInt("watchRegion");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getSetAsAdmin() {
        return setAsAdmin;
    }

    public void setSetAsAdmin(int setAsAdmin) {
        this.setAsAdmin = setAsAdmin;
    }

    public String getValidcode() {
        return validcode;
    }

    public void setValidcode(String validcode) {
        this.validcode = validcode;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPortrait() {
        return portrait;
    }

    public void setPortrait(String portrait) {
        this.portrait = portrait;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAppIdentity() {
        return appIdentity;
    }

    public void setAppIdentity(String appIdentity) {
        this.appIdentity = appIdentity;
    }

    public String getAppUserNum() {
        return appUserNum;
    }

    public void setAppUserNum(String appUserNum) {
        this.appUserNum = appUserNum;
    }

    public String getCellPhone() {
        return cellPhone;
    }

    public void setCellPhone(String cellPhone) {
        this.cellPhone = cellPhone;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public int getRelation() {
        return relation;
    }

    public void setRelation(int relation) {
        this.relation = relation;
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public String getOldCellPhone() {
        return oldCellPhone;
    }

    public void setOldCellPhone(String oldCellPhone) {
        this.oldCellPhone = oldCellPhone;
    }

    public int getWatchRegion() {
        return watchRegion;
    }

    public void setWatchRegion(int watchRegion) {
        this.watchRegion = watchRegion;
    }

    public static void getList(Context context, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            HttpUtil.post(CommonRestPath.getUserList(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

//    public static void add(Context context, Keeper keeper, final HttpCallback callback) {
//        try {
//            JSONObject json = new JSONObject();
//            json.put("id", "0");
//            json.put("name", keeper.getName());
//            json.put("password", keeper.getPassword());
//            json.put("portrait", keeper.getPortrait());
//            json.put("appUserNum", keeper.getAppUserNum());
//            json.put("appIdentity", keeper.getAppIdentity());
//            json.put("validcode", keeper.getValidcode());
//            json.put("cellPhone", keeper.getCellPhone());
//            json.put("nickName", keeper.getNickName());
//            json.put("relation", keeper.getRelation());
//            json.put("watchRegion", keeper.getWatchRegion());
//            HttpUtil.post(CommonRestPath.userADD(), json, new JsonHttpResponseHelper(callback).getHandler());
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
//    }

    public void update(Context context, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            json.put("id", this.getId());
            json.put("name", this.getName());
            json.put("password", this.getPassword());
            json.put("portrait", this.getPortrait());
            json.put("appUserNum", getSetAsAdmin());
            json.put("appIdentity", this.getAppIdentity());
            json.put("validcode", getValidcode());
            json.put("cellPhone", getCellPhone());
            json.put("nickName", getNickName());
            json.put("relation", getRelation());
            json.put("watchRegion", getWatchRegion());
            HttpUtil.post(CommonRestPath.userADD(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure("Exception");
            } catch (Exception e) {
                Log.e(Constants.TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public void setAscensionMain(Context context, boolean isArise, final HttpCallback callback) {
        try {
            JSONObject json = new JSONObject();
            json.put("ascensionId", id);
            json.put("isArise", isArise ? 1 : 0);
            HttpUtil.post("/identityUser/ascensionMain", json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure("Exception");
            } catch (Exception e) {
                Log.e(Constants.TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    public void delete(Context context, final HttpCallback callback) {
        try {
            JSONArray array = new JSONArray();
            array.put(this.getId());
            JSONObject json = new JSONObject();
            json.put("ids", array);
            HttpUtil.post(CommonRestPath.DeleteUser(), json, new JsonHttpResponseHelper(callback).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                callback.onFailure("Exception");
            } catch (Exception e) {
                Log.e(Constants.TAG, AppContext.getContext().getString(R.string.callback_err), e);
            }
        }
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Keeper{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", portrait='").append(portrait).append('\'');
        sb.append(", password='").append(password).append('\'');
        sb.append(", appIdentity='").append(appIdentity).append('\'');
        sb.append(", appUserNum='").append(appUserNum).append('\'');
        sb.append(", cellPhone='").append(cellPhone).append('\'');
        sb.append(", oldCellPhone='").append(oldCellPhone).append('\'');
        sb.append(", nickName='").append(nickName).append('\'');
        sb.append(", validcode='").append(validcode).append('\'');
        sb.append(", imei='").append(imei).append('\'');
        sb.append(", relation=").append(relation);
        sb.append(", watchRegion=").append(watchRegion);
        sb.append('}');
        return sb.toString();
    }
}
